﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using ProxyWrapper;
using System.Text;
using System.Data;
public partial class Account_Users : SecPage
{
    protected void Page_Load(object sender, EventArgs e)
    {
        try
        {
            if (Session["userObject"] == null)
            {
                Response.Redirect("Default.aspx");
            }
            else
            {
                if (Request.QueryString != null && Request.QueryString["msg"] != null)
                {
                    showMessages.Visible = true;
                    mmsg.InnerText = Request.QueryString["msg"].ToString();
                }
                string sortOrder = Enums.UserSorting.time.ToString();
                if (Request.QueryString != null && Request.QueryString["order"] != null)
                {
                    sortOrder = Request.QueryString["order"].ToString();
                }
                DrawPage((Enums.UserSorting)Enum.Parse(typeof(Enums.UserSorting), sortOrder));
            }
        }
        catch (Exception ex)
        {
            Response.Redirect("Default.aspx?error="+ex.Message);
        }
    }

    private void DrawPage(Enums.UserSorting sortorder)
    {
        ProxyWrapper.UserProxy.UserDetails objUser = ((ProxyWrapper.UserProxy.UserDetails)Session["userObject"]);
        ProxyWrapper.UserServiceWrapper _usr = new UserServiceWrapper();
        List<ProxyWrapper.UserProxy.UserDetails> objUserList = _usr.GetUserList(objUser.ClientId, objUser.HashCode);
        switch (sortorder)
        {
            case Enums.UserSorting.time:
                objUserList = objUserList.OrderByDescending(m => m.CreateDate).ToList<ProxyWrapper.UserProxy.UserDetails>();
                dateSorter.Style.Add("font-weight", "bolder");
                dateSorter.Style.Add("color", "#000");
                break;
            case Enums.UserSorting.login:
                objUserList = objUserList.OrderByDescending(m => m.LastLogin).ToList<ProxyWrapper.UserProxy.UserDetails>();
                loginSorter.Style.Add("font-weight", "bolder");
                loginSorter.Style.Add("color", "#000");
                break;
            case Enums.UserSorting.alpha:
                objUserList = objUserList.OrderBy(m => m.UserName).ToList<ProxyWrapper.UserProxy.UserDetails>();
                nameSorter.Style.Add("font-weight", "bolder");
                nameSorter.Style.Add("color", "#000");
                break;
            case Enums.UserSorting.service:
                objUserList = objUserList.OrderByDescending(m => m.GetServiceList.Tables[0].Rows.Count).ToList<ProxyWrapper.UserProxy.UserDetails>();
                serviceSorter.Style.Add("font-weight", "bolder");
                serviceSorter.Style.Add("color", "#000");
                break;
            default:
                objUserList = objUserList.OrderByDescending(m => m.CreateDate).ToList<ProxyWrapper.UserProxy.UserDetails>();
                dateSorter.Style.Add("font-weight", "bolder");
                dateSorter.Style.Add("color", "#000");
                break;
        }
        if (objUserList != null && objUserList.Count > 0 && objUserList[0].UserId > 0)
        {
            // objUser.
            userCount.InnerText = "Showing 1 - " + objUserList.Count.ToString() + " of " + objUserList.Count.ToString() + " staffs";
            lstUsers.DataSource = objUserList;
            lstUsers.DataBind();
        }
        else
        {
            lstUsers.DataSource = new object[] { };
            lstUsers.DataBind();
        }
    }

    public string GetServiceCount(string userId)
    {
        List<ProxyWrapper.UserProxy.UserDetails> objUserList = ((List<ProxyWrapper.UserProxy.UserDetails>)lstUsers.DataSource);
       // Dictionary<int, string> dcServices = objUserList.First(m => m.UserId.ToString() == userId).GetServiceList;
        DataTable dcServices = objUserList.First(m => m.UserId.ToString() == userId).GetServiceList.Tables[0];
        if (dcServices != null && dcServices.Rows.Count > 0)
            return "<span class='numberOfServices'>" + dcServices.Rows.Count.ToString() + " Buttons...</span>" + GetServiceDetails(dcServices);
        else
            return "<i>Not associated with any buttons.</i>";

    }

    private string GetServiceDetails(DataTable dcServices)
    {
        StringBuilder sb = new StringBuilder();
        if (dcServices != null)
        {
            sb.Append("<span class='serviceList' style='display: none;'>");
            for (int i = 0; i < dcServices.Rows.Count; i++)
            {
                sb.Append(" <a href='Services.aspx?serviceId=" + dcServices.Rows[i][0].ToString() + "'>" + dcServices.Rows[i][1].ToString() + "</a> ");
                sb.Append(",");
            }
            
        }
        if (sb != null && sb.Length > 0)
        {
            sb = sb.Remove(sb.Length - 1, 1);
        }
        sb.Append("</span>");
        sb.Append("    <a class='sClickToShow' style='text-decoration:underline;'>Click to show</a>");
        return sb.ToString();
    }
    //public string GetServiceDetails(string userId)
    //{
    //    StringBuilder sb = new StringBuilder();
    //    List<ProxyWrapper.UserProxy.UserDetails> objUserList = ((List<ProxyWrapper.UserProxy.UserDetails>)lstUsers.DataSource);
    //    Dictionary<int, string> dcServices = objUserList.First(m => m.UserId.ToString() == userId).GetServiceList;
    //    if (dcServices != null)
    //    {
    //        sb.Append("<span class='serviceList' style='display: none;'>");
    //        foreach (KeyValuePair<int, string> entry in dcServices)
    //        {
    //            sb.Append("<a href=Services.aspx?serviceId=" + entry.Key.ToString() + ">" + entry.Value.ToString() + "</a>");
    //            sb.Append(",");
    //        }
    //    }
    //    if (sb != null && sb.Length > 0)
    //    {
    //        sb = sb.Remove(sb.Length - 1, 1);
    //    }
    //    sb.Append("</span>");
    //    sb.Append("<a class='sClickToShow' style='text-decoration:underline;'>Click to show</a>");
    //    return sb.ToString();
    //}
}